package com.bootdo.system.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.AppVersionDO;
import com.bootdo.system.service.AppVersionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys/appV")
public class AppVersionController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final String prefix = "system/appV";

    @Autowired
    private AppVersionService appVersionService;

    @GetMapping()
    @RequiresPermissions("sys:appV:appV")
    String appV(Model model){
        return prefix + "/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("sys:appV:appV")
    PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<AppVersionDO> list = appVersionService.list(query);
        int total = appVersionService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("sys:appV:upload")
    String add(Model model){
        return prefix + "/add";
    }

    @PostMapping("/upload")
    @RequiresPermissions("sys:appV:upload")
    @ResponseBody
    public R upload(@RequestParam("appPackage") MultipartFile appPackage,String versionNumber,
                                String sign,String mandatoryUpdate){
        try {
            return appVersionService.uploadAppPackage(appPackage,versionNumber,sign,mandatoryUpdate);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>uploadAPPPackage接口报错:未知错误-{}，错误栈-{}<<<<<<<<<<",e.getMessage(), e);
            return R.error(-1,"系统繁忙");
        }
    }

    @RequiresPermissions("sys:appV:download")
    @GetMapping("/edit/{id}")
    String editById(Model model, @PathVariable("id") Long id) {
        model.addAttribute("appVersion", appVersionService.get(id));
        return prefix + "/edit";
    }

    @RequiresPermissions("sys:appV:download")
    @GetMapping("/edits")
    String edit(Model model) {
        model.addAttribute("appVersion", appVersionService.downloadAppPackage());
        return prefix + "/edit";
    }






}
